package example.demo.controller;

import example.demo.config.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 开始发消息  c测试确认  确认发布模式
 */
@RestController
@RequestMapping("confirm")
@Slf4j
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * localhost:8080/confirm/sendMessage/大家好1
     * @param message
     * 发布确认交换机
     */
    @GetMapping("/sendMessage/{message}")
    public void sendMessage(@PathVariable String message){
        // 消息确认回调,正常的
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(
                ConfirmConfig.CONFIRM_EXCHANGE_NAME,
                ConfirmConfig.CONFIRM_ROUTING_KEY,
                message+"key1",
                correlationData);
        log.info("发送消息的内容为：{}",message+"key1");


        // 消息确认回调1  错误的
        CorrelationData correlationData1 = new CorrelationData("2");
        rabbitTemplate.convertAndSend(
                ConfirmConfig.CONFIRM_EXCHANGE_NAME,
                ConfirmConfig.CONFIRM_ROUTING_KEY+"2",
                message+"key12",
                correlationData1);
        log.info("发送消息的内容为：{}",message+"key12");
    }
}
